﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Configuration;
using System.Data.OleDb;

namespace Zatvor
{
    /// <summary>
    /// Summary description for zatvor
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class zatvor : System.Web.Services.WebService
    {

        [WebMethod]
        public void dodajVoZatvor(int igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            string sql = "INSERT INTO [Zatvorenici] ([Kazna],[bonus],[igracID]) VALUES ( @kazna,@freePass,@pID)";
            DBCon.Open();
            OleDbCommand cmd = new OleDbCommand(sql, DBCon);

            cmd.Parameters.AddWithValue("@kazna", "3");
            cmd.Parameters.AddWithValue("@freePass", "0");
            cmd.Parameters.AddWithValue("@pID", igracID);           

            string prasalnik = "SELECT * FROM Zatvorenici";

            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int pom = 0;
            while (i.Read())
            {
                if (String.Compare(igracID.ToString(), i.GetValue(5).ToString()) == 0 )
                {
                    pom = 1;
                }
            }
            if (pom == 0)
            {
                cmd.ExecuteNonQuery();
            }
            DBCon.Close();
        }

        [WebMethod]
        public bool proveriZatvor(int igracID)
        {
            bool dozvola = true;
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();
            
            string prasalnik = "SELECT * FROM Zatvorenici";

            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int pom = 0;
            while (i.Read())
            {
                if (String.Compare(igracID.ToString(), i.GetValue(5).ToString()) == 0)
                {
                    pom++;
                }
            }

            if (pom > 0)
            {
                dozvola = false;
            }

            DBCon.Close();

            return dozvola;
        }

        [WebMethod]
        public void dodajIgrac(int igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            string sql = "INSERT INTO [Bonusi] ([playerID],[bonus]) VALUES ( @pID,@bon)";
            DBCon.Open();
            OleDbCommand cmd = new OleDbCommand(sql, DBCon);
           
            cmd.Parameters.AddWithValue("@pID", igracID);
            cmd.Parameters.AddWithValue("@bon", "0");            
            
            cmd.ExecuteNonQuery();
            
            DBCon.Close();
        }

        [WebMethod]
        public int vratiKazna(string igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();
            string prasalnik = "SELECT * FROM Zatvorenici";
            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int br = 0;
            while (i.Read())
            {
                if (String.Compare(igracID, i.GetValue(5).ToString()) == 0)
                {
                    br = Convert.ToInt32(i.GetValue(3).ToString());
                }
            }
            DBCon.Close();

            return br;
        }


        [WebMethod]
        public void platiBanka(string igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();

            string prasalnik = "SELECT * FROM Zatvorenici";
            string update = "UPDATE Zatvorenici SET Kazna=k WHERE ID=?";
            string del = "DELETE * FROM Zatvorenici WHERE ID=?";
            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbCommand komandaupdate = new OleDbCommand(update, DBCon);
            OleDbCommand komandadelete = new OleDbCommand(del, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int id = 0;

            while (i.Read())
            {
                if (String.Compare(igracID, i.GetValue(5).ToString()) == 0)
                {
                    id = Convert.ToInt32(i.GetValue(0).ToString());
                }
            }
            komandaupdate.Parameters.AddWithValue("k", 0);
            komandaupdate.Parameters.AddWithValue("?", id);
            komandaupdate.ExecuteNonQuery();
            komandadelete.Parameters.AddWithValue("?", id);
            komandadelete.ExecuteNonQuery();


            DBCon.Close();
        }

        [WebMethod]
        public void nePadnaaDupli(string igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();

            string prasalnik = "SELECT * FROM Zatvorenici";
            string update = "UPDATE Zatvorenici SET Kazna=k WHERE ID=?";
            string del = "DELETE * FROM Zatvorenici WHERE ID=?";
            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbCommand komandaupdate = new OleDbCommand(update, DBCon);
            OleDbCommand komandadelete = new OleDbCommand(del, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int id = 0;
            int dz = 0;

            while (i.Read())
            {
                if (String.Compare(igracID, i.GetValue(5).ToString()) == 0)
                {
                    id = Convert.ToInt32(i.GetValue(0).ToString());
                    dz = Convert.ToInt32(i.GetValue(3).ToString());
                }
            }

            if (dz == 0)
            {
                komandadelete.Parameters.AddWithValue("?", id);
                komandadelete.ExecuteNonQuery();
                //Button1.Visible = false;
                //Button2.Visible = false;
                //ButtonOslobodi.Visible = true;
            }

            if (dz >= 1)
            {
                dz = dz - 1;

                komandaupdate.Parameters.AddWithValue("k", dz);
                komandaupdate.Parameters.AddWithValue("?", id);
                komandaupdate.ExecuteNonQuery();

            }

            DBCon.Close();
        }



        [WebMethod]
        public void voZatvor(string igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();

            string prasalnik = "SELECT * FROM Zatvorenici";
            string update = "UPDATE Zatvorenici SET Kazna=k WHERE ID=?";
            string del = "DELETE * FROM Zatvorenici WHERE ID=?";
            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbCommand komandaupdate = new OleDbCommand(update, DBCon);
            OleDbCommand komandadelete = new OleDbCommand(del, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int id = 0;
            int dz = 0;

            while (i.Read())
            {
                if (String.Compare(igracID, i.GetValue(5).ToString()) == 0)
                {
                    id = Convert.ToInt32(i.GetValue(0).ToString());
                    dz = Convert.ToInt32(i.GetValue(3).ToString());
                }
            }

            if (dz == 0)
            {
                komandadelete.Parameters.AddWithValue("?", id);
                komandadelete.ExecuteNonQuery();
            }
            if (dz >= 1)
            {
                dz = dz - 1;
                if (dz == 0)
                {
                    komandadelete.Parameters.AddWithValue("?", id);
                    komandadelete.ExecuteNonQuery();
                }
                else
                {
                    komandaupdate.Parameters.AddWithValue("k", dz);
                    komandaupdate.Parameters.AddWithValue("?", id);
                    komandaupdate.ExecuteNonQuery();
                }
            }

            DBCon.Close();
        }


        [WebMethod]
        public void oslobodiOdZatvor(string igracID)
        {
            String cStr = ConfigurationManager.ConnectionStrings["konekcija"].ConnectionString;
            OleDbConnection DBCon = new OleDbConnection(cStr);
            DBCon.Open();

            string prasalnik = "SELECT * FROM Zatvorenici";
            string update = "UPDATE Zatvorenici SET Kazna=k WHERE ID=?";
            string del = "DELETE * FROM Zatvorenici WHERE ID=?";
            OleDbCommand komanda = new OleDbCommand(prasalnik, DBCon);
            OleDbCommand komandaupdate = new OleDbCommand(update, DBCon);
            OleDbCommand komandadelete = new OleDbCommand(del, DBCon);
            OleDbDataReader i = komanda.ExecuteReader();
            int id = 0;

            while (i.Read())
            {
                if (String.Compare(igracID, i.GetValue(5).ToString()) == 0)
                {
                    id = Convert.ToInt32(i.GetValue(0).ToString());
                }
            }
            komandaupdate.Parameters.AddWithValue("k", 0);
            komandaupdate.Parameters.AddWithValue("?", id);
            komandaupdate.ExecuteNonQuery();
            komandadelete.Parameters.AddWithValue("?", id);
            komandadelete.ExecuteNonQuery();

            DBCon.Close();
        }

    }
}
